package com.hydra.auth.config.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;

public class CustomExceptionTranslator extends DefaultWebResponseExceptionTranslator {

    @Override
    public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
        ResponseEntity<OAuth2Exception> translate = super.translate(e);
        OAuth2Exception body = translate.getBody();
        if(body == null){
            return null;
        }
        BootOAuth2Exception bootOAuth2Exception = new BootOAuth2Exception(body.getHttpErrorCode(), e.getMessage(), e);

        return new ResponseEntity<>(bootOAuth2Exception, translate.getHeaders(), translate.getStatusCode());
    }

}
